<?php
$ma_nganh=$_GET['ma_nganh'];
include('../../configs/connect.php');   
 mysql_query("SET NAMES 'UTF8'");
 $ma_nganh=rtrim($ma_nganh,','); 
$mang=explode(',',$ma_nganh);

$sql="Select a.ma_monhoc FROM tbl_mon_ctdt a,tbl_ctdt b WHERE a.ma_ctdt = b.ma_ctdt";
         
for($i=0;$i<count($mang);$i++){
    $sql="Select a.ma_monhoc FROM tbl_mon_ctdt a,tbl_ctdt b 
                             WHERE a.ma_ctdt = b.ma_ctdt 
                             AND madm_nganh='".$mang[$i]."' 
                             AND a.ma_monhoc IN($sql)";
}         

   $sql="Select dm_monhoc.madm_mh,ten_mh From dm_monhoc,(SELECT madm_mh,count(madm_mh) as solan 
                                         FROM (Select DISTINCT madm_mh,khoiluong From dm_monhoc,tbl_mon_ctdt 
                                                WHERE  dm_monhoc.madm_mh=tbl_mon_ctdt.ma_monhoc AND madm_mh IN($sql)) 
                                                as aaaa group by madm_mh having solan=1) as monhocchung 
                                                WHERE  dm_monhoc.madm_mh=monhocchung.madm_mh";
  

$q=mysql_query($sql);
echo mysql_error();
$myarray=array();
$str="";
while($nt=mysql_fetch_array($q)){
$str=$str ."\"$nt[madm_mh]\"".","."\"$nt[ten_mh]\"".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";

?>